﻿$(document).ready(function () {
    // Style the text input elements
    $("input[type=text], input[type=password], textarea")
      .attr('class', 'text')
      .focus(function () {
          $(this).addClass('text-active');
          $(this).removeClass('text');
      })
      .blur(function () {
          $(this).addClass('text');
          $(this).removeClass('text-active');
      }
    );
    $("input[type=button], input[type=submit]").not("input[class]")
      .addClass('button');

    $("*[data-confirm]").click(function () {
        return confirm($(this).attr('data-confirm'));
    });

    // Do data-attribute processing
    var selection = 'input[type=text][data-hint], input[type=password][data-hint], textarea[data-hint]';
    $(selection).each(
    function () {
        var elem = $(this);
        if (!elem.val()) {
            elem.attr('displaysHint', "true");
            elem.val(elem.attr('data-hint'));
            elem.css('color', '#999');
        }
    })
    .focus(function () {
        var elem = $(this);
        if (elem.attr('displaysHint')) {
            elem.attr('displaysHint', null);
            elem.val("");
            elem.css('color', '');
        }
    })
    .blur(function () {
        var elem = $(this);
        if (elem.val().trim() == "") {
            elem.val(elem.attr("data-hint"));
            elem.attr('displaysHint', "true");
            elem.css('color', '#aaa');
        } else {
            elem.attr('displaysHint', null);
        }
    });

    $('input[type=submit][data-hint], input[type=button][data-hint]').each(function () {
        var elem = $(this);
        elem.attr('displaysHint', null);
    })
    .mousemove(function (e) {
        var elem = $(this);
        var message;
        if (!elem.attr('displaysHint')) {
            message = $(document.createElement('div'));
            message.attr('class', 'hint_message');
            message.attr('id', 'hint-message');
            message.css('display', 'inline-block').css('position', 'absolute');
            message.html(elem.attr('data-hint'));
            $('body').append(message);
        } else {
            message = $("#hint-message");
        }
        message.css('left', e.pageX + 5).css('top', e.pageY + 5);

        elem.attr('displaysHint', "true");
    })
    .mouseleave(function () {
        var elem = $(this);
        var message = $("#hint-message");
        message.remove();
        elem.attr('displaysHint', null);
    });

    $('*[data-required]').each(function () {
        var elem = $(this);
        var parentForm = elem.closest('form');
        if (parentForm && !parentForm.attr('alreadyAdded')) {
            parentForm.attr('alreadyAdded', "true");
            parentForm.submit(function (e) {
                var missing = false;
                $(this).find('*[data-required]').each(function () {
                    var msg = $("*[data-required-message-for=" + $(this).attr('id') + "]");
                    if ($(this).attr('displaysHint') || $(this).val().trim() == "") {
                        missing = true;
                        if (msg)
                            msg.html($(this).attr("data-required"));
                    } else if (msg)
                        msg.html("");
                });
                if (missing) {
                    if (parentForm.attr('data-missing-input') && parentForm.attr('data-error-id')) {
                        $("#" + parentForm.attr('data-error-id')).html(parentForm.attr('data-missing-input'));
                    }
                    e.preventDefault();
                }
            });
        }
    });
});